<?php // -*-coding:utf-8; mode:php-mode;-*-
/**
 * @file   LoginCtrl.php
 * @author koocyton <koocyton@gmail.com>
 * @date   Mon Dec 14 14:57:16 2009
 * 
 * @brief  Ctrl 的基础类
 *
 * @package       LoginCtrl
 * @subpackage    CtrlBase
 */

namespace ctrl;

use framework\mvc\view\smarty;

use framework\util;
use \util\Functions;
use \view\RedirectView;

/** 
 * 用户登录的 ctrl
 *
 * @package       LoginCtrl
 * @subpackage    CtrlBase
 */
class LoginCtrl extends CtrlBase {

	/** 
	 * 构造函数，继承父方法
	 * 
	 * @return void
	 * @access public
	 */
	public function __construct()
	{
		parent::__construct();
	}
	
	/** 
	 * 用户在登录操作前，此方法先执行
	 * 
	 * @return boolean Success
	 * @access public
	 */
	function beforeFilter()
	{
		if (!empty($_SESSION['loginId']) && !empty($_SESSION['loginName']) && $this->dispatcher->ctrlMethodName!="quit")
		{
			header("Location: ./");
			return false;
		}
		return true;
	}

	/** 
	 * 用户退出
	 * 
	 * @return void
	 * @access public
	 */
	function quit()
	{
		$_SESSION = null;
		if (isset($_COOKIE[session_name()])) {
			setcookie(session_name(), '', time()-42000, '/');
		}

		return new RedirectView("?act=Login.form");
	}
	
	/** 
	 * 输出用户登录的界面
	 * 
	 * @return view\SmartView Object
	 * @access public
	 */
	public function form()
	{
		return new smarty\SmartyView("Login.form.html");
	}

	/** 
	 * 用户提交登录
	 * 
	 * @return void
	 * @access public
	 */
	public function post()
	{
		if ($_SERVER['REQUEST_METHOD']=="POST" && !empty($_POST['loginName']) && !empty($_POST['loginPass']))
		{
			$userService = util\Singleton::get("service\\UserService");
			$user = $userService->login($_POST['loginName'], $_POST['loginPass']);

			$_SESSION['loginId']   = $user->id;
			$_SESSION['loginName'] = $user->loginName;
		}
		else {
			throw new \Exception('请将表单填写完整');
		}
	}
}